package threads;

import java.io.*;
import java.util.StringTokenizer;

import data.CustomerInfo;
import rm2.DRSserverinit;
import rm2.UDPDatagram;
//import exception.ShowIsFullException;
import exception.BuyToManyException;
//import exception.ShowIsFullException;
import exception.ItemNotExistsException;
//import exception.ShowRegisteredException;
//import exception.ShowTakenException;
//import exception.NoPreRequisiteException;
import exception.CustomerNotExistsException;
//import exception.

public class BuyItem// extends Thread
{
	String customer, item;
	int number;
	CustomerInfo cl;
	ItemAddition ca;
	DRSserverinit drSserverinit;
	 
	public BuyItem(String customer_id, String item_id, int number, DRSserverinit drSserverinit) 
	{
		// TODO Auto-generated constructor stub
		this.customer = customer_id;
		this.item = item_id;
		//this.ca = ca;
		this.number = number;
		this.drSserverinit=drSserverinit;
		//this.start();
	}
	
	public String Buy()
	{	
		String message = "0";
		try
		{
			
			if(drSserverinit.slist.get(item) == null)
			{
				String reply; 
				reply = UDPDatagram.messageSendAndReceive("buy~" + this.customer + "~" + item + "~" + this.number,
						this.drSserverinit.closest_1_server_udp_port, this.drSserverinit.closest_1_server_address);
				
				if(reply.contains("unsuccessful"))
				{
					reply = UDPDatagram.messageSendAndReceive("buy~" + this.customer + "~" + item + "~" + this.number,
							this.drSserverinit.closest_2_server_udp_port, this.drSserverinit.closest_2_server_address);
				}
				
				if(reply.contains("unsuccessful"))
					throw new ItemNotExistsException(this.customer + " --> Buying" + ": " + "Item: " + item + " does not exist.");
				else
					message = Integer.toString(this.number);
			}
			
			if(number > drSserverinit.slist.get(this.item).getCapacity())
				throw new BuyToManyException(this.customer  +" --> Buying" + ": " + this.item+" Either out of stock or too many items .");
		
			//File file = new File("input\\CustomerFile" + File.separator + customer);
			File file = new File(this.drSserverinit  + File.separator + "CustomerFile" + File.separator + customer);
			String customer="";

			String reserved="";
			int numberOfItems = 0;
			
			if (file.exists())
			{			
				
					ca = new ItemAddition(this.drSserverinit);					
					
					synchronized (drSserverinit.slist.get(this.item)) 
					{
						ca.addItem(this.customer, this.item, this.number);
						message =Integer.toString(this.number);
					}				
					
					
					System.out.println(this.customer + " bought " + this.number + " unit(s) of " + this.item);

			}
			else
				System.out.println("customer doesnt exist");
				message = Integer.toString(0);
				//throw new CustomerNotExistsException(this.customer  +" --> " + ": " + " Customer does not exist.");
			//End of FileExist
		}
		catch (Exception e) 
		{
			System.out.println(e.getMessage());
		}

		return message;
	}

}
